﻿using System;
using System.IO;
using Csu.Modsim.ModsimModel;

namespace iwpm.optimization
{
    public class HydroDebugger
    {
        private bool _failed; 
        private StreamWriter _sw;

        public bool FailedToOpen
        {
            get
            {
                return _failed;
            }
        }

        public HydroDebugger(string outputfile, IHydroDebuggable hydroObject)
        {
            try
            {
                _sw = new StreamWriter(outputfile);
                _failed = false;
            }
            catch
            {
                _sw = null;
                _failed = true;
            }
            hydroObject.WritingDebug += new EventHandler<MessageEventArgs>(_hydroObject_WritingDebug);
        }

        private void _hydroObject_WritingDebug(object sender, MessageEventArgs e)
        {
            if (_sw != null)
                _sw.WriteLine(e.Message); 
        }

        public void Close() 
        {
            if (_sw != null) 
                _sw.Close();
            _sw = null;
        }
        
    }
}
